package com.daixiaowei.service;

import com.daixiaowei.model.Restaurant;
import com.daixiaowei.repository.RestaurantRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

public class RestaurantServiceTest {

    @InjectMocks
    private RestaurantService restaurantService; // 要测试的服务

    @Mock
    private RestaurantRepository restaurantRepository; // 模拟的数据访问层

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.openMocks(this); // 初始化Mockito注解
    }

    @Test
    public void testGetAllRestaurants() {
        // 准备模拟的数据
        List<Restaurant> mockRestaurants = new ArrayList<>();
        mockRestaurants.add(new Restaurant("欢乐餐厅", "商业街", "休闲餐"));
        mockRestaurants.add(new Restaurant("惊喜餐厅", "市中心", "特色美食"));

        // 设置模拟行为
        when(restaurantRepository.findAll()).thenReturn(mockRestaurants);

        // 调用服务方法
        List<Restaurant> restaurants = restaurantService.getAllRestaurants();

        // 验证结果
        assertEquals(2, restaurants.size());
        assertEquals("欢乐餐厅", restaurants.get(0).getName());
        assertEquals("惊喜餐厅", restaurants.get(1).getName());
    }
}
